package apps.softtech.fastmarket.adapter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import apps.softtech.Utility.ApplicationConstants;
import apps.softtech.Utility.RequestDataFromServer;
import apps.softtech.fastmarket.R;
import apps.softtech.fastmarket.model.Product;

public class AdapterFollowProduct extends BaseAdapter{

	ArrayList<Product> mProductsList = new ArrayList<Product>();
	LayoutInflater inf;
	Context mContext;
	DisplayImageOptions options;
	
	public AdapterFollowProduct(Context mContext, ArrayList<Product> mProductsList,DisplayImageOptions options ){
		this.mContext = mContext;
		this.mProductsList = mProductsList;
		this.options = options;
		inf = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	@Override
	public int getCount() {
		return mProductsList.size();
	}
	@Override
	public Object getItem(int arg0) {
		return null;
	}
	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View vi, ViewGroup arg2) {
		if(mProductsList.size()==0||mProductsList==null){
			vi = inf.inflate(R.layout.search_no_result, null);
			return vi;
		}
		ViewHolder vh=null;
		if(vi==null){
			vi = inf.inflate(R.layout.item_my_profile_follow_product, null);
			vh = new ViewHolder();
			vh.ivProduct = (ImageView)vi.findViewById(R.id.ivMyProfileFolloProduct);
			vh.tvProductYour = (TextView) vi.findViewById(R.id.tvMyProfileFollowProduct);
			vh.btSetFollow = (Button)vi.findViewById(R.id.btSetFollowProduct);
			vi.setTag(vh);
		}
		else{
			vh = (ViewHolder) vi.getTag();
		}
		vh.tvProductYour.setText(""+mProductsList.get(position).getPrice() + mProductsList.get(position).getProductId());
	
		String url =ApplicationConstants.BASE_URL+ mProductsList.get(position).getAvatarPath();
		ImageLoader.getInstance().displayImage(url, vh.ivProduct, options);
		final int  id = position;
		vh.btSetFollow.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				int idProduct = mProductsList.get(id).getProductId();
				new AsyncTaskGetFollowProduct(id,idProduct).execute();
			}
		});
		return vi;
	}
	class ViewHolder{
		ImageView ivProduct;
		TextView tvProductYour;
		Button btSetFollow;
	}
	class AsyncTaskGetFollowProduct extends AsyncTask<Void, Void, Void>{
		ProgressDialog pd;
		int id;
		int idProduct;
		int sucssec;
		public AsyncTaskGetFollowProduct(int id2, int idProduct2) {
			id = id2;
			idProduct = idProduct2;
		}
		@Override
		protected void onPreExecute() {
			pd = new ProgressDialog(mContext);
			pd.setMessage("UnFollowing....");
			pd.show();
			super.onPreExecute();
		}
		@Override
		protected Void doInBackground(Void... params) {
			try {
				JSONObject json = new JSONObject(new RequestDataFromServer().requestUnFollowProduct(2, idProduct));
				sucssec = json.getInt("success");
			} catch (JSONException e) {
				e.printStackTrace();
			}
			
			return null;
		}
		@Override
		protected void onPostExecute(Void result) {
			if(sucssec==1){
				mProductsList.remove(id);
				notifyDataSetChanged();
			}else{
				Toast.makeText(mContext, "Not UnFollow", Toast.LENGTH_LONG).show();
			}
			pd.dismiss();
			super.onPostExecute(result);
		}
		
	}

}
